var arr = new Array();
var isall=false;
var role;
$(function(){
	
	/**
	 * 获取风琴菜单
	 */
	$.ajax({
         type: 'GET',
         dataType: "json",
         url: '/roleuser/getRole',
         success: function (data) {
             $.each(data, function (i, m) {
            	var ahtml = '<ul class="ulcls" id="'+m.roleid+'">';
    			 ahtml+='<li  class="selected"><a href="javascript:void(0);" class="roleusera" onclick="selectRole(\''+m.roleid+'\')">'+m.rolename+'</a></li>';
    			 ahtml+='</ul>';
				 $("#rolemenu").append(ahtml);
				 if(i==0){
					 role=m.roleid;
					 $("#"+m.roleid).addClass('select');
				 }
             });
            setTree();
         }
     });
})
function setTree(){
	$("#menutree").tree({
  		url:'rolemenu/getUseMenu?roleid='+role,
  		id:'id',
  		text:'text',
  		cascadeCheck:true,
  		attributes:'attributes',
  		checkbox:true
  	});
}
/**
 * 选择角色
 * @param roleid
 */
function selectRole(roleid){
	$("ul").removeClass("select");
	$("#"+roleid).addClass('select');
	role=roleid;
	setTree();
}
/**
 * 保存
 */
function save(){
	var nodes=$("#menutree").tree('getChecked');
	var ids='';
	for(var i=0;i<nodes.length;i++){
		var target=nodes[i].domId;
		ids+=nodes[i].id+',';
		var parentid=nodes[i].attributes;
		if(parentid!=''&&parentid!=null&&parentid!=undefined){
			ids+=nodes[i].attributes+',';
			var node=$("#menutree").tree('find',parentid);
			if(node){
				if($("#menutree").tree('getParent',node.target)){
					var pnode=$("#menutree").tree('getParent',node.target);
					if(pnode){
						ids+=pnode.id+",";
					}
				}
			}
		}
	}
	var vo={};
	vo.roleid=role;
	vo.ids=ids;
	$.ajax({
		type:'post',
		url: '/rolemenu/saveRoleMenu',
		data:vo,
		success:function(data){
			if(data.code==200){
				$.messager.alert("成功","保存成功",'info',function(){
					setTree();
				});
			}
			else{
				$.messager.alert("失败","保存失败："+data.msg,'error');
			}
		},
		error:function(data){
			$.messager.alert("失败","保存失败：",'error');
		}
	});
}


